﻿(function (ns, $) {
    ZYCLib.RegNameSpace(ns).Handdler = (function () {
        var _LetterUrl = "";//拼音查找地址 
        var _LetterTarget = "_blank";//拼音.1本页跳转，2弹出页面(默认)

        var initPage = function (paramsJson, url, targetType) {

            //Params.PageIndex = paramsJson.PageIndex;
            //Params.MBID = paramsJson.MBID;
            //Params.SupplierID = paramsJson.SupplierID;
            //Params.Keyword = paramsJson.Keyword;
            //Params.MName = paramsJson.MName;
            //Params.ShopName = paramsJson.ShopName;

            if (_LetterUrl == "" && url != null && url != "") {
                _LetterUrl = url;
            }

            if (targetType == 1) {
                _LetterTarget = "_self";
            }
        };

        //点击字母
        var getCodexLetter = function (letter) {
            ZYCLib.Web.JsonAjax.PostData("common/MCodexService/GetCodexLetter",
                { letter: letter },
                function (ret) {
                    var letterHtml = "";
                    for (var i = 0; i < ret.ListLetter.length; i++) {
                        var eachLetter = ret.ListLetter[i];
                        if (i == 0) {
                            letterHtml += " <a href=\"javascript:void(0);\" sign='subletter' onclick=\"ZYCLib.Breeds.Handdler.GetCodexNameByLetter('" + eachLetter + "');\"  class=\"selected\" >" + eachLetter + "</a> ";
                        } else {
                            letterHtml += " <a href=\"javascript:void(0);\" sign='subletter' onclick=\"ZYCLib.Breeds.Handdler.GetCodexNameByLetter('" + eachLetter + "');\">" + eachLetter + "</a> ";
                        }
                    }
                    $("#divMCodexSubLetters").html(letterHtml);
                },
                function () {
                });
        };

        //点击字母组合
        var getCodexNameByLetter = function (letter) {
            ZYCLib.Web.JsonAjax.PostData("common/MCodexService/GetCodexNameByLetter",
                { letter: letter, url: _LetterUrl },
                function (ret) {
                    var codexHtml = "";
                    for (var i = 0; i < ret.length; i++) {
                        var eachCodex = ret[i];
                        codexHtml += "<a href=\""+ ZYCLib.HttpWWWDomain +"/jh" + eachCodex.MBID + ".html\" sign='codexname' codexid=\"" + eachCodex.MBID + "\">" + eachCodex.MName + "</a>";
                    }
                    $("#divPinyinMCodex").html(codexHtml);
                },
            function () {
                $("#divMCodexSubLetters").find(".selected").attr("class", "");
                $.each($("#divMCodexSubLetters").children(),function(i,item) {
                    if (item.innerHTML == letter) {
                        $(item).attr("class", "selected");
                    } 
                });
                if ($("#divMCodexSubLetters").find(".selected").length == 0) {
                    $($("#divMCodexSubLetters").children()[0]).attr("class", "selected");
                }
            });
        };
        var getSubLetter = function (obj, letter) {

            $(obj).siblings().removeAttr("class");
            $(obj).attr("class", "selected");

            $("#divMCodexSubLetters").css("display", "block");
            $("#divPinyinMCodex").css("display", "block");

            getCodexLetter(letter);
            getCodexNameByLetter(letter);
        };

        var getMCodexProperty = function (objDom) {

            ZYCLib.UI.Form.SubmitByAjax(
                "PropertyDiv",
                {
                    Action: "common/MCodexService/GetMCodexByProperty",
                    Validate: function () {
                        return true;
                    },
                    WarpData: function (data) {
                        data["PropertyID"] = $(objDom).attr("proid");
                    },
                    OnSuccess: function (respondData) {
                        if (respondData.length > 0) {
                            $("#PropertyName").html($(objDom).html());
                            $("#PropertyDiv").removeClass("none");
                        }
                        var dom = "";
                        for (var i = 0; i < respondData.length; i++) {
                            dom += "<li><a target=\"_blank\" href=\"" + ZYCLib.HttpWWWDomain + "/jh" + respondData[i].MBID + ".html\" mbid=\"" + respondData[i].MBID + "\" title=\"" + respondData[i].MName + "\">" + respondData[i].MName + "</a></li>";
                        }
                        $("#proData").html(dom);
                    },
                    //OnComplete: function (data) {
                    //},
                    //OnErrorCodeHandler:function() {
                    //}
                }
            );
        };

        return {
            InitPage: initPage,
            GetCodexNameByLetter: getCodexNameByLetter,
            GetSubLetter: getSubLetter,
            GetMCodexProperty: getMCodexProperty
        };
    })();
})("Breeds", jQuery)